import { HttpClient } from '@angular/common/http';
import { Inject, inject, Injectable } from '@angular/core';
import { Observable, share, shareReplay } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class HttpService {
  http = inject(HttpClient);
  getHello(flag: boolean) {
    let options = {};
    if (flag) {
      options = {
        responseType: 'text' as 'json',
      };
    }
    return this.http.get<string>('/api', options);
  }
  private catchData!: Observable<string>;
  getCatchData() {
    if (!this.catchData) {
      this.catchData = this.http
        .get<string>('/api', {
          responseType: 'text' as 'json',
        })
        .pipe(shareReplay(1));
    }
    return this.catchData;
  }
  private onceData!: Observable<string>;
  getOnceData() {
    if (!this.onceData) {
      this.onceData = this.http
        .get<string>('/api', {
          responseType: 'text' as 'json',
        })
        .pipe(share());
    }
    return this.onceData;
  }

  getList(word: string): Observable<string[]> {
    return this.http.get<string[]>('/api/getList', {
      params: { word },
      responseType: 'json',
    });
  }
}
